﻿using System;
using System.Collections.Generic;
using System.Text;

using RichServerInfo.DeviceInfo;
using ValidateCommon;
using System.Net;
using System.IO;
using System.Xml;
using System.Collections;
using System.Globalization;
using System.Web;

namespace RichWebServiceDll
{
    public class RichWSRegist
    {/// <summary>
        /// 返回状态
        /// </summary>
        public string RequestResultState { get; set; }

        /// <summary>
        /// 返回信息
        /// </summary>
        public string RequestResultMessage { get; set; }

        public string RequestImpowerNo { get; set; }

        public RichWSRegist(string strServerIp, string strWebName, string strWebUrl,string strDiskNo)
        {
            try
            {
                var builderWebServiceAddress = new StringBuilder();
                var objSysinfo = new SystemInfo();
                var strUpdate = UTF8code.RepleaseDES(EncryptUtily.EncryptUtily.LoginEncrypt(DateTime.Now.ToString(CultureInfo.InvariantCulture))); //网站上传日期
                //var strPowerNo = UTF8code.RepleaseDES(EncryptUtily.EncryptUtily.LoginEncrypt(objSysinfo.GetMainHardDiskId()));
                var strPowerNo = UTF8code.RepleaseDES(strDiskNo);
                var strName = UTF8code.RepleaseDES(EncryptUtily.EncryptUtily.LoginEncrypt("CMSValidate"));
                var strPsd = UTF8code.RepleaseDES(EncryptUtily.EncryptUtily.LoginEncrypt("Validate2012.JsbRich"));
                builderWebServiceAddress.Append("http://" + strServerIp + "/IWebSewvices/SetWebSiteInfo.asmx/GetClientData");
                builderWebServiceAddress.Append("?strWebName=" + UTF8code.RepleaseDES(EncryptUtily.EncryptUtily.LoginEncrypt(strWebName)));
                builderWebServiceAddress.Append("&strWebUrl=" + UTF8code.RepleaseDES(EncryptUtily.EncryptUtily.LoginEncrypt(strWebUrl)));
                builderWebServiceAddress.Append("&strUpdate=" + strUpdate);
                builderWebServiceAddress.Append("&strPowerNo=" + strPowerNo);
                builderWebServiceAddress.Append("&strWebPath=" + UTF8code.RepleaseDES(EncryptUtily.EncryptUtily.LoginEncrypt(HttpContext.Current.Server.MapPath("~/"))));
                builderWebServiceAddress.Append("&strValidateName=" + strName);
                builderWebServiceAddress.Append("&strValidatePsd=" + strPsd);
                var webrequest = WebRequest.Create(builderWebServiceAddress.ToString());
                var webresponse = webrequest.GetResponse();

                var stream = webresponse.GetResponseStream();
                if (stream != null)
                {
                    var sr = new StreamReader(stream, Encoding.UTF8);
                    //读取返回流
                    var xd = new XmlDocument();
                    xd.Load(sr);
                    var strresponseString = xd.ChildNodes[1].InnerText.ToString(CultureInfo.InvariantCulture);
                    var resultHashtable = AnalysisWebResponse(strresponseString);
                    RequestResultState = resultHashtable["RequestResultState"].ToString();
                    RequestResultMessage = resultHashtable["RequestResultMessage"].ToString();
                    RequestImpowerNo = resultHashtable["RequestImpowerNo"].ToString();
                }
                else
                {
                    RequestResultState = "0";
                    RequestResultMessage = "未获取返回信息";
                    RequestImpowerNo = "服务器错误";
                }
            }
            catch (Exception ex)
            {
                throw new Exception("提交服务器数据时异常:" + ex.Message.Replace("\r\n", ""));
            }
        }

        /// <summary>
        /// 处理返回字符数据并进行分割
        /// </summary>
        /// <param name="strWebResponse">webservice响应</param>
        /// <returns>分割后的哈希值对</returns>
        private static Hashtable AnalysisWebResponse(string strWebResponse)
        {
            strWebResponse = EncryptUtily.EncryptUtily.LoginDecrypt(strWebResponse);
            var responseResult = strWebResponse.Split('_');

            var returnResult = new Hashtable();
            returnResult["RequestResultState"] = responseResult[0];
            returnResult["RequestResultMessage"] = responseResult[1];
            returnResult["RequestImpowerNo"] = responseResult[2];
            return returnResult;
        }
    }
}
